#include <iostream>
#include <cstdlib>
#include <string>

int main() {
    std::cout << "=== 项目环境变量测试程序 ===" << std::endl;
    std::cout << "测试PROJECT_ENV_CONFIG功能是否正确设置环境变量到当前进程" << std::endl;
    
    // 测试env_config.sh中定义的环境变量
    const char* test_vars[] = {
        "PROJECT_NAME",
        "PROJECT_VERSION", 
        "BUILD_TYPE",
        "CUSTOM_FLAG",
        "PROJECT_ROOT",
        "COMPILER_FLAGS",
        "DEBUG_MODE",
        "LOG_LEVEL"
    };
    
    bool found_any = false;
    int expected_count = 0;
    int found_count = 0;
    
    // 预期的环境变量值（来自env_config.sh）
    const char* expected_values[] = {
        "MyAwesomeProject",  // PROJECT_NAME
        "1.0.0",            // PROJECT_VERSION
        "Release",          // BUILD_TYPE
        "ENABLE_FEATURE_X", // CUSTOM_FLAG
        "/home/roy/workspace/my_open_source/builder", // PROJECT_ROOT
        "-O2 -DNDEBUG",     // COMPILER_FLAGS
        "false",            // DEBUG_MODE
        "INFO"              // LOG_LEVEL
    };
    
    int total_vars = sizeof(test_vars) / sizeof(test_vars[0]);
    
    std::cout << "\n检查环境变量:" << std::endl;
    for (int i = 0; i < total_vars; i++) {
        const char* var_name = test_vars[i];
        const char* expected = expected_values[i];
        const char* actual = std::getenv(var_name);
        
        expected_count++;
        
        if (actual) {
            if (std::string(actual) == std::string(expected)) {
                std::cout << "✅ " << var_name << "=" << actual << " (正确)" << std::endl;
                found_count++;
                found_any = true;
            } else {
                std::cout << "⚠️  " << var_name << "=" << actual << " (期望: " << expected << ")" << std::endl;
                found_any = true;
            }
        } else {
            std::cout << "❌ " << var_name << " (未设置，期望: " << expected << ")" << std::endl;
        }
    }
    
    std::cout << "\n=== 测试结果 ===" << std::endl;
    std::cout << "总计: " << expected_count << " 个环境变量" << std::endl;
    std::cout << "正确: " << found_count << " 个" << std::endl;
    std::cout << "错误: " << (expected_count - found_count) << " 个" << std::endl;
    
    if (found_count == expected_count) {
        std::cout << "\n🎉 所有环境变量都正确设置！PROJECT_ENV_CONFIG功能正常工作。" << std::endl;
        return 0;
    } else if (found_any) {
        std::cout << "\n⚠️  部分环境变量设置不正确，请检查配置脚本。" << std::endl;
        return 1;
    } else {
        std::cout << "\n❌ 未检测到任何项目环境变量" << std::endl;
        std::cout << "提示: 请设置 PROJECT_ENV_CONFIG 环境变量指向配置脚本" << std::endl;
        return 1;
    }
}